package com.dao;

import org.hibernate.Session;

import entities.*;
import com.util.*;
public class SearchUsers{

	public boolean searchByNameAndCode(String name,String code) {
		Session session=null;
		try{
		session=HbnUtil.getSession();
		session.beginTransaction();
		Users u=(Users)session.createQuery("from Users as u where u.email='"+name+"' and u.password='"+code+"'").uniqueResult();
		if(u!=null){
			return true;
		}
		session.beginTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			session.beginTransaction().rollback();
		}finally{
			HbnUtil.releaseSession(session);
		}
		
		return true;
	}
	public Users searchByEmail(String email){
		Session session=null;
		Users u=null;
		try{
		session=HbnUtil.getSession();
		session.beginTransaction();
		u=(Users)session.createQuery("select * from Users as u where u.email='"+email+"'").uniqueResult();
		session.beginTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			session.beginTransaction().rollback();
		}finally{
			HbnUtil.releaseSession(session);
		}
		return u;
	}
	public static boolean searchEmailAndPass(String email, String password)
	{
		Session session=null;
		try{
		session = HbnUtil.getSession();
		session.beginTransaction();
		Users u = (Users)session.createQuery("from Users as u where u.email='"+email+"' and u.password='"+password+"'").uniqueResult();
		if(u != null)
			return true;
		session.beginTransaction().commit();
	}catch(Exception e){
		e.printStackTrace();
		session.beginTransaction().rollback();
	}finally{
		HbnUtil.releaseSession(session);
	}
	
	return false;
	}
	
	public static Users searchUser(String email, String password)
	{
		Session session=null;
		try{
		session = HbnUtil.getSession();
		session.beginTransaction();
		Users u = (Users)session.createQuery("from Users as u where u.email='"+email+"' and u.password='"+password+"'").uniqueResult();
		if(u != null)
		{
			session.beginTransaction().commit();
			HbnUtil.releaseSession(session);
			return u;
		}
		session.beginTransaction().commit();
	}catch(Exception e){
		e.printStackTrace();
		//session.beginTransaction().rollback();
	}finally{
		HbnUtil.releaseSession(session);
	}
	
	return null;
	}
	public boolean searchByAccount(String account){
		Session session=null;
		Users u=null;
		try{
		session=HbnUtil.getSession();
		session.beginTransaction();
		u=(Users)session.createQuery("from Users as u where u.userAccount='"+account+"'").uniqueResult();
		if(u!=null)
			return true;
		session.beginTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			session.beginTransaction().rollback();
		}finally{
			HbnUtil.releaseSession(session);
		}
		return false;
	}

}
